package com.wcar.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wcar.common.entity.*;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.*;
import com.wcar.store.service.PayMoneyService;
import com.wcar.store.vo.CouponVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-06-06  11:19
 */
@Service
public class PayMoneyServiceImpl implements PayMoneyService {
    @Resource
    private CarWashRecordDao carWashRecordDao;
    @Resource
    private StantionDao stantionDao;
    @Resource
    private UserBalanceDao userBalanceDao;
    @Resource
    private UserBalanceLogDao userBalanceLogDao;
    @Resource
    private UserIntegralDao userIntegralDao;
    @Resource
    private WashCarCouponDao washCarCouponDao;
    @Resource
    private UserIntegralLogDao userIntegralLogDao;
    @Override
    public Result<Integer> payMoney(CouponVo couponVo) {
        System.out.println(couponVo);
        //判断钱是否够
        QueryWrapper<UserBalance> wrapper=new QueryWrapper<>();
        wrapper.eq("store_id",couponVo.getStoreId());
        wrapper.eq("user_id",couponVo.getUserId());
        UserBalance userBalance = userBalanceDao.selectOne(wrapper);
        BigDecimal bigDecimalValue = new BigDecimal(Double.toString(couponVo.getTotalAmount()));
        if(userBalance.getAmount().compareTo(bigDecimalValue)<0){
            return new Result<>(900,"账户余额不足",null);
        }
        //修改订单
        CarWashRecord carWashRecord = new CarWashRecord();
        carWashRecord.setOrderId(couponVo.getOrderId());
        if(couponVo.getId()!=null){
            carWashRecord.setProductId(couponVo.getId());
            carWashRecord.setIsUsed("1");
            QueryWrapper<WashCarCoupon> wrapper1=new QueryWrapper<>();
            wrapper1.eq("id",couponVo.getId());
            WashCarCoupon washCarCoupon = washCarCouponDao.selectOne(wrapper1);
            carWashRecord.setPayMoney(Double.valueOf(couponVo.getPayMoney()-washCarCoupon.getCouponMoney()));
        }else {
            carWashRecord.setIsUsed("0");
            carWashRecord.setPayMoney(couponVo.getTotalAmount());
        }
        carWashRecord.setEndTime(new Date());
        carWashRecord.setUseTime(couponVo.getUseTime());
        carWashRecord.setTotalAmount(couponVo.getTotalAmount());
        carWashRecord.setWashStatus("1");
        carWashRecordDao.updateById(carWashRecord);
        //修改工位状态
        stantionDao.updateIdles(couponVo.getStationId());
        //扣除余额
        UpdateWrapper<UserBalance> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("store_id",couponVo.getStoreId())
                .eq("user_id",couponVo.getUserId()).setSql("amount=amount-"+bigDecimalValue);
        userBalanceDao.update(updateWrapper);
        //添加扣除余额日志
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setUserId(couponVo.getUserId());
        userBalanceLog.setStoreId(couponVo.getStoreId());
        userBalanceLog.setExpenditure(bigDecimalValue.intValue());
        userBalanceLog.setExpenditureTime(new Date());
        userBalanceLogDao.insert(userBalanceLog);
        //增加积分
        QueryWrapper<UserIntegral> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",couponVo.getUserId());
        queryWrapper.eq("store_id",couponVo.getStoreId());
        UserIntegral userIntegral = userIntegralDao.selectOne(queryWrapper);
        if(userIntegral!=null){
            UpdateWrapper<UserIntegral> userIntegralUpdateWrapper=new UpdateWrapper<>();
            userIntegralUpdateWrapper.eq("store_id",couponVo.getStoreId())
                    .eq("user_id",couponVo.getUserId()).setSql("credits=credits+"+bigDecimalValue.intValue());
            userIntegralDao.update(userIntegralUpdateWrapper);
        }else {
            UserIntegral userIntegral1 = new UserIntegral();
            userIntegral1.setUserId(couponVo.getUserId());
            userIntegral1.setCredits(bigDecimalValue);
            userIntegral1.setSource("洗车消费");
            userIntegral1.setIntegralTime(new Date());
            userIntegral1.setStoreId(couponVo.getStoreId());
            userIntegralDao.insert(userIntegral1);
        }
        //增加积分日志
        UserIntegralLog userIntegralLog = new UserIntegralLog();
        userIntegralLog.setUserId(couponVo.getUserId());
        userIntegralLog.setStoreId(couponVo.getStoreId());
        userIntegralLog.setExpendScore(0);
        userIntegralLog.setArrivalScore(bigDecimalValue.intValue());
        userIntegralLog.setRechargeTime(new Date());
        userIntegralLogDao.insert(userIntegralLog);
        return new Result<>(200,"结算成功",null);
    }
}
